import os, sqlite3

class DB():
    def __init__(self, dbpath=None):
        self.conn = ''
        if dbpath:
            self.db = dbpath
        else:
            self.db = os.path.dirname(__file__)+'/../data/urldata.db'
            print self.db

    def start(self):
        try:
            self.conn = sqlite3.connect(self.db, check_same_thread = False)
            return True
        except sqlite3.Error,e:
            print e.message
        return False

    def select(self, sql):
        cursor = self.conn.execute(sql)
        return cursor

    def insert(self, sql, params=()):
        if params:
            self.conn.execute(sql, params)
        else:
            self.conn.execute(sql)

        try:
            res = self.conn.commit()
        except Exception as e:
            print e.message

        return res

    def close(self):
        self.conn.close()